// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_PRODUCT_TEHINIC_HISTORY
// * CreateDate    : 2011/9/27 13:27:06
// * Creater       : CodeSmith
// * LastChangeDate: 2011/9/27 13:27:06
// * LastChanger   : 2011/9/27 13:27:06
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdProductTehinicHistory
    /// </summary>
    public partial class ProdProductTehinicHistoryBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdProductTehinicHistoryEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdProductTehinicHistoryEntity GetProdProductTehinicHistory(ProdProductTehinicHistoryEntity objProdProductTehinicHistoryEntity)
        {
            return DataAccess.SelectSingle<ProdProductTehinicHistoryEntity>(objProdProductTehinicHistoryEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdProductTehinicHistoryEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdProductTehinicHistory(ProdProductTehinicHistoryEntity objProdProductTehinicHistoryEntity)
        {
            return DataAccess.Insert(objProdProductTehinicHistoryEntity);
        }

        public static bool InsertProdProductTehinicHistory(List<ProdProductTehinicHistoryEntity> objProdProductTehinicHistoryEntityList)
        {
            return DataAccess.Insert(objProdProductTehinicHistoryEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdProductTehinicHistoryEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdProductTehinicHistory(ProdProductTehinicHistoryEntity objProdProductTehinicHistoryEntity)
        {
            return DataAccess.Update(objProdProductTehinicHistoryEntity);
        }

        public static bool UpdateProdProductTehinicHistory(List<ProdProductTehinicHistoryEntity> objProdProductTehinicHistoryEntityList)
        {
            return DataAccess.Update<ProdProductTehinicHistoryEntity>(objProdProductTehinicHistoryEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdProductTehinicHistoryEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdProductTehinicHistory(ProdProductTehinicHistoryEntity objProdProductTehinicHistoryEntity)
        {
            return DataAccess.Delete(objProdProductTehinicHistoryEntity);
        }

        public static bool DeleteProdProductTehinicHistory(List<ProdProductTehinicHistoryEntity> objProdProductTehinicHistoryEntityList)
        {
            return DataAccess.Delete<ProdProductTehinicHistoryEntity>(objProdProductTehinicHistoryEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdProductTehinicHistoryAll()
        {
            return DataAccess.SelectAllDataSet<ProdProductTehinicHistoryEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdProductTehinicHistoryEntity> GetProdProductTehinicHistoryAllList()
        {
            return DataAccess.SelectAll<ProdProductTehinicHistoryEntity>();
        }
        #endregion

        #region Functions
        public static List<ProdProductTehinicHistoryEntity> GetHistoryFlows(string ProductID)
        {
            if (string.IsNullOrEmpty(ProductID))
                return null;
            string Sql = "SELECT * FROM PROD_PRODUCT_TEHINIC_HISTORY WHERE PRODUCT_ID='" + ProductID + "' ";
            return DataAccess.Select<ProdProductTehinicHistoryEntity>(Sql);
        }

        public static bool SaveFlowHistory(List<ProdProductTechnicEntity> ListFlowDetail, ProdProductTehinicHistoryEntity FlowHistory)
        {
            bool bFlag = false;
            if (ListFlowDetail != null && ListFlowDetail.Count > 0 && FlowHistory != null)
            {
                using (DataAccessBroker broker = DataAccessFactory.Instance())
                {
                    try
                    {
                        ProdProductTehinicHistoryEntity History = FlowHistory;
                        History.HistoryId = Guid.NewGuid().ToString();
                        History.CreatedDate = DateTime.Now;
                        List<ProdProductTehinicHistoryItemEntity> list = new List<ProdProductTehinicHistoryItemEntity>();
                        for (int i = 0; i < ListFlowDetail.Count; i++)
                        {
                            ProdProductTehinicHistoryItemEntity mm = new ProdProductTehinicHistoryItemEntity();
                            mm.ItemId = Guid.NewGuid().ToString();                            
                            mm.HistoryId = History.HistoryId;
                            mm.TechnicId = ListFlowDetail[i].TechnicId;
                            mm.GroupId = ListFlowDetail[i].GroupId;
                            mm.Sequence = i;
                            mm.PlanWorkingHour = ListFlowDetail[i].PlanWorkingHours;
                            mm.Coefficient = ListFlowDetail[i].Coefficient;
                            mm.Remark = ListFlowDetail[i].Remark;
                            list.Add(mm);
                        }
                        broker.BeginTransaction();
                        DataAccess.Insert(History,broker);
                        DataAccess.Insert<ProdProductTehinicHistoryItemEntity>(list, broker);
                        broker.Commit();
                    }
                    catch (Exception ex)
                    {
                        broker.RollBack();
                        throw ex;
                    }
                }
            }
            return bFlag;
        }
        #endregion
    }


}

